package com.yuntongxun.ecdemo.voicechat.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.common.utils.CommomUtil;
import com.yuntongxun.ecdemo.common.utils.DateUtil;
import com.yuntongxun.ecdemo.voicechat.bean.Favorites;
import com.yuntongxun.ecdemo.voicechat.listeners.IRecyclerViewItemListener;
import com.yuntongxun.ecdemo.voicechat.listeners.IRecyclerViewItemLongListener;
import com.yuntongxun.ecdemo.voicechat.util.CommonUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnLongClick;


/**
 * Created by Will on 12/9/2015.
 */
public class FavoritesAdapter extends RecyclerView.Adapter {

    private Context mContext;

    private List<Favorites> notes;

    private IRecyclerViewItemLongListener mListener;

    public FavoritesAdapter(Context context, List<Favorites> noteList, IRecyclerViewItemLongListener myItemClickListener) {

        mContext = context;

        this.notes = noteList;

        mListener = myItemClickListener;
    }

    @Override
    public int getItemCount() {
        return notes.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
        View v = View.inflate(viewGroup.getContext(), R.layout.adapter_favorites_list_item, null);

        return new HomeFragmentViewHolder(v);
    }

    @Override
    public int getItemViewType(int position) {
        return position % 4;  //any logic can go here
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        HomeFragmentViewHolder holder = (HomeFragmentViewHolder) viewHolder;

        Favorites favorites = notes.get(position);

        holder.favoritesCollectionerIv.setText(favorites.getSendUser());
        holder.favoritesCollectionTimeTv.setText(CommonUtils.getFormatDate(favorites.getCreateTime()));

        if(1 == favorites.getType()){
            //语音
            holder.favoritesImageIv.setVisibility(View.GONE);
            holder.favoritesVoiceIv.setVisibility(View.VISIBLE);

        }else {
            //图片
            holder.favoritesImageIv.setVisibility(View.VISIBLE);
            holder.favoritesVoiceIv.setVisibility(View.GONE);

            if(!CommonUtils.isEmpty(favorites.getUrl())) {
                Picasso.with(mContext)
                        .load(favorites.getUrl())
                        .placeholder(R.drawable.ic_register_logo)
                        .tag(mContext)
                        .into(holder.favoritesImageIv);
            }
        }

    }

    public class HomeFragmentViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.favorites_collectioner_iv)
        TextView favoritesCollectionerIv;
        @InjectView(R.id.favorites_collection_time_tv)
        TextView favoritesCollectionTimeTv;
        @InjectView(R.id.favorites_image_iv)
        ImageView favoritesImageIv;
        @InjectView(R.id.favorites_voice_iv)
        ImageView favoritesVoiceIv;

        public HomeFragmentViewHolder(View itemView) {
            super(itemView);

            ButterKnife.inject(this, itemView);
        }

        @OnClick(R.id.favorites_select_bg_layout)
        public void clickItem() {
            if (mListener != null) {
                mListener.onItemClick(getPosition());
            }
        }
        @OnLongClick(R.id.favorites_select_bg_layout)
        public boolean onLongItemClick(){
            if (mListener != null) {
                mListener.onItemLongClick(getPosition());
                return true;
            }

            return false;
        }
    }
}
